package game;

import controller.GameController;
import entities.*;
import util.Mp3Player;
import view.ChessBoard;

import javax.swing.*;
import java.awt.*;

/***
 * Author: Mr.Zhao, Time: 2018/5/1/21:38
 * Description: 启动单人模式
 */
public class Game {
    private GameController gameController;

    public Game(GameController gameController) {
        this.gameController = gameController;
    }

    public static void main(String[] args) throws Exception {

        //最初面板
        ChessBoard chessBoard = ChessBoard.getInstance();
        GameController gameController = new GameController(chessBoard);
        gameController.initChessBord(BasePiece.RED_PIECE);

        chessBoard.addMouseListener(gameController);
        JFrame frame = new JFrame();
        frame.setVisible(true); //先设置可见使inset可被获取到，从而使frame与panel大小一致
        frame.setTitle("中国象棋");
        Insets insets = frame.getInsets();
        frame.setSize(chessBoard.getWidth() + insets.right,
                chessBoard.getHeight() + insets.top);
        frame.add(chessBoard);

        frame.setLayout(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);// 在屏幕中间显示

        new Thread(new GameDriver()).start();
    }


    /**
     * 一个内部类，驱动游戏不停播放音频
     *
     * @author 赵永远
     */
    public static class GameDriver implements Runnable {
        public void run() {
            Mp3Player mp3Player = new Mp3Player();
            mp3Player.play("bg.mp3");
            while (mp3Player.getPlayer().isComplete()) {
                mp3Player.play("bg.mp3");
            }
        }
    }

}
